package Jisuanqi;


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Timer;  //Timer用于管理在后台执行的延迟任务或周期性任务
import java.util.TimerTask;  //其中的任务是用java.util.TimerTask表示

import javax.swing.BorderFactory;//边框
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;


public class Test_Interface extends JFrame{

    private static final long serialVersionUID = 1L;  //定义程序序列化,1L是默认定义

    private int max = 50;
    private int a = 0;
    private int b = 0;
    private int c = 0;
    private int count = 1;

    private JLabel JLgross = new JLabel("共10页");
    private JLabel JLpage = new JLabel();  //页码控制
    private JLabel jl1 = new JLabel("  "); //插两个空格进去
    private JLabel JLtime = new JLabel("倒计时:");
    private JLabel JLhour = new JLabel();
    private JLabel JLminute = new JLabel();
    private JLabel JLseconds = new JLabel();

    private JButton JB1 = new JButton("提交考卷");
    private JButton b1 = new JButton("首页");
    private JButton b2 = new JButton("上一页");
    private JButton b3 = new JButton("下一页");
    private JButton b4 = new JButton("尾页");

    private JLabel jl[] = new JLabel[50]; //50个题目标签
    private JTextField jtf[] = new JTextField[50];//50个文本框，存储答案

    private JPanel pnl1 = new JPanel();
    private JPanel pnl2 = new JPanel();
    private JPanel pnl3 = new JPanel();
    private JPanel pnl4 = new JPanel();

    static CardLayout care = new CardLayout();

    int[] answer=new int[max];
    String[] studentAnswer=new String[max];

    private File file = null;
    private FileWriter fw1 = null;

    public long time = 5400;

    private void countDown() {

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {

                time--;

                long hour = time / 3600;
                long minute = (time - hour * 3600) / 60;
                long seconds = time - hour * 3600 - minute * 60;

                JLhour.setText(hour + "时");
                JLminute.setText(minute + "分");
                JLseconds.setText(seconds + "秒");


                if( time == 0 ) {

                    JB1.doClick(); //调用提交试卷按钮

                }
            }
        }, 0, 1000);
    }


    private void Event() {

        //提交试卷按钮
        JB1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int correctAnswer=0;

                for(int i=0,k=0;i<max;i++,k++){

                    studentAnswer[i]=jtf[k].getText().trim();

                    try {
                        if(Integer.parseInt(studentAnswer[i]) == answer[k]){
                            //将string字符串类型转换为integer整数类型
                            correctAnswer++;
                        }
                    }catch(NumberFormatException u) {

                    }
                }

                int score = 100*(int)correctAnswer/max;

                setVisible(false);
                dispose();

                String s="共50道题\n";
                s=s+"答对"+correctAnswer+"道题\n";
                s=s+"答错"+(max-correctAnswer)+"道题\n";
                s=s+"成绩"+String.format("%d",score)+"分\n";

                file = new File("信息收集.txt");
                if(!file.exists()){
                    try {
                        file.createNewFile();
                    }catch (IOException u1) {
                        u1.printStackTrace();
                    }
                }

                try {
                    fw1 = new FileWriter(file, true);

                    fw1.write("姓名    " + Login_Interface.s1 + "    专业    " + Login_Interface.s3
                            + "    班级    " +  Login_Interface.s2 + "    考试得分：" + score + "\n");

                    fw1.close();

                }catch(IOException u2) {
                    u2.printStackTrace();
                }

                JOptionPane.showMessageDialog(null, s,"考试结束",JOptionPane.ERROR_MESSAGE);

            }
        });

        //首页
        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.first(pnl3);
                count = 1;
                JLpage.setText("第" + count + "页");
            }
        });

        //前一页
        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.previous(pnl3);
                if ( count > 1 && count <=10) {
                    count --;
                }else {
                    count = 10 ;
                }
                JLpage.setText("第" + count + "页");
            }
        });

        //下一页
        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.next(pnl3);
                if ( count >= 1 && count < 10 ) {
                    count ++;
                }else {
                    count = 1 ;
                }
                JLpage.setText("第" + count + "页");
            }
        });

        //尾页
        b4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                care.last(pnl3);
                count = 10;
                JLpage.setText("第" + count + "页");
            }
        });

    }


    public Test_Interface() {

        this.setTitle("开始考试");
        this.setSize(550, 400);
        this.setLocationRelativeTo(null);  //将此窗口置于屏幕的中央
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout(20,20)); //设置 各组件间水平垂直间隔为20像素

        countDown();

        pnl1.add(JLhour);
        pnl1.add(JLminute);
        pnl1.add(JLseconds);

        JLpage.setText("第" + count + "页");

        pnl2.setLayout(new FlowLayout());
        pnl2.add(JLgross);
        pnl2.add(JLpage);//页数
        pnl2.add(jl1);
        pnl2.add(JLtime);
        pnl2.add(pnl1);
        pnl2.add(JB1);

        this.add(pnl2,BorderLayout.NORTH);

        pnl3.setLayout(care);
        this.add(pnl3,BorderLayout.CENTER);
        pnl3.setBorder(BorderFactory.createLoweredBevelBorder()); //边框

        for(int i = 0 , k = 0; i < 10 ; i++ ) {

            JPanel p = new JPanel();
            p.setLayout(new GridLayout(5,2,20,20)); //网格布局5行2列，水平垂直间距都设为20像素

            for(int j = 0 ; j < 5 ; j++) {

                a=(int)(Math.random()*100+1);
                b=(int)(Math.random()*100+1);
                c=(int)(Math.random()*100+1);

                jl[k] = new JLabel(a + "+" + b + "-" + c + "=");
                jl[k].setFont(new Font("黑体",Font.BOLD,15));

                p.add(jl[k]);
                answer[k] = a + b - c;

                jtf[k] = new JTextField(6);
                p.add(jtf[k]);

                k++;
            }

            pnl3.add(p);
        }

        pnl4.setLayout(new GridLayout(1,4));

        pnl4.add(b1);
        pnl4.add(b2);
        pnl4.add(b3);
        pnl4.add(b4);

        this.add(pnl4, BorderLayout.SOUTH);

        //美化界面
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Event();

        setVisible(true);

    }


//	public static void main(String[] args){
//
//		new Test_Interface();
//
//	}
}

